<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;

/**
 * 
 */
class Nav extends Controller
{
	
	public function index()
	{
		$list = DB::name('sys_menu')->where('pid = 0 and is_del = 0')->paginate(20);
		$page = $list->render();
		$this->assign('page', $page);
		$list = $list->all();
		foreach ($list as $k => $v) {
			$list[$k]['child'] = DB::name('sys_menu')->where('pid = '.$v['id'])->select();
		}
		return $this->fetch('index',[
			'list'		=>	$list,
		]);
	}


	public function create()
	{
		$ord = DB::name('sys_menu')->max('orderid');
		$ord += 1;
		if(Request::instance()->isPost()){
			$data['menu_name']  = Request::instance()->post('title');
			$data['pid']	= Request::instance()->post('parentid');
			$data['icon']	= Request::instance()->post('icon');
			$data['url']	= Request::instance()->post('url');
			$data['status']	= Request::instance()->post('status'); 
			$type = Request::instance()->post('int/a');
			$data['type']	= implode(',', $type);
			$data['add_time'] = time();
			$data['orderid'] = $ord;
			$res = DB::name('sys_menu')->insert($data);
			if($res)
			{
				$this->success('添加菜单成功',url('index/index/index',array('con'=>'nav','act'=>'index')));
			}else{
				$this->error('添加失败');
			}
		}

		$group = DB::name('authgroup')->order('id asc')->select();
		$menu  = DB::name('sys_menu')->field('id,menu_name')->where('pid = 0')->select();
		return $this->fetch('create',[
			'group'		=> 	$group,
			'ord'		=>  $ord,
			'menu'		=>  $menu,
		]);
	}


	public function update($id)
	{
		if(Request::instance()->isPost()){
			$data['menu_name']  = Request::instance()->post('title');
			$data['pid']	= Request::instance()->post('parentid');
			$data['icon']	= Request::instance()->post('icon');
			$data['url']	= Request::instance()->post('url');
			$data['status']	= Request::instance()->post('status');
			$id = Request::instance()->post('id'); 
			$type = Request::instance()->post('int/a');
			$data['type']	= implode(',', $type);
			$data['add_time'] = time();
			$data['orderid'] = Request::instance()->post('orderid');
			$res = DB::name('sys_menu')->where('id = '.$id)->setField($data);
			if($res)
			{
				$this->success('修改菜单成功',url('index/index/index',array('con'=>'nav','act'=>'index')));
			}else{
				$this->error('修改失败');
			}
		}
		$data = DB::name('sys_menu')->where('id = '.$id)->find();
		$data['type'] = explode(',', $data['type']);
		$group = DB::name('authgroup')->order('id asc')->select();
		$menu  = DB::name('sys_menu')->field('id,menu_name')->where('pid = 0')->select();
		return $this->fetch('update',[
			'data'		=>	$data,
			'group'		=> 	$group,
			'menu'		=>  $menu,
		]);		
	}	

	public function delNav()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'nav_del');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('sys_menu')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}
	}
}
?>